export default class DB {
    private dbName: string // 数据库名称
    private db: any // 数据库对象
    constructor(dbName: string) {
        this.dbName = dbName
    }

    // 打开数据库
    public openStore(stores:any) {
        const request = window.indexedDB.open(this.dbName, 3)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log('数据库打开成功')
                this.db = event.target.result
                console.log(event)
                resolve(true)//在数据库打开之后即resolve之后，才会getList
            }
            request.onerror = (event) => {
                console.log('数据库打开失败')
                console.log(event)
                reject(event)
            }
            request.onupgradeneeded = (event) => {
                console.log('数据库升级成功')
                const {result}: any = event.target
                for (const storeName in stores) {//初始化多个objectStore对象仓库
                    const {keyPath, indexs} = stores[storeName]
                    if (!result.objectStoreNames.contains(storeName)) {//没有则新建表
                        const store = result.createObjectStore(storeName, {autoIncrement: true, keyPath})
                        if (indexs && indexs.length) {
                            indexs.map((v: string) =>
                                // createIndex可以新建索引，unique字段是否唯一
                                store.createIndex(v, v, {unique: false})
                            )
                        }
                        store.transaction.oncomplete = (e: any) => {
                            console.log('创建对象仓库成功')
                        }
                    }
                }
            }
        })
    }
    // 新增/修改数据库数据
    updateItem(storeName: string, data: any) {
        console.log(this.db)
        const store = this.db.transaction([storeName], 'readwrite').objectStore(storeName)
        const request = store.put({
            ...data,
            updateTIme: new Date().getTime()
        })
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log('数据写入成功')
                console.log(event)
                resolve(event)
            }
            request.onerror = (event: any) => {
                console.log('数据写入失败')
                console.log(event)
                reject(event)
            }
        })
    }
    // 删除数据
    deleteItem(storeName: string, key: number | string) {
        const store = this.db.transaction([storeName], 'readwrite').objectStore(storeName)
        const request = store.delete(key)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log('数据删除成功')
                console.log(event)
                resolve(event)
            }
            request.onerror = (event: any) => {
                console.log('数据删除失败')
                console.log(event)
                reject(event)
            }
        })
    }
    // 查询所有数据
    getList(storeName: string) {
        const store = this.db.transaction(storeName).objectStore(storeName)
        const request = store.getAll()
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log('查询所有数据成功')
                console.log(event.target.result)
                resolve(event.target.result)
            }
            request.onerror = (event: any) => {
                console.log('查询所有数据失败')
                console.log(event)
                reject(event)
            }
        })
    }
    // 查询某一条数据
    getItem(storeName: string, key: number | string) {
        const store = this.db.transaction(storeName).objectStore(storeName)
        const request = store.get(key)
        return new Promise((resolve, reject) => {
            request.onsuccess = (event: any) => {
                console.log('查询某一条数据成功')
                console.log(event.target.result)
                resolve(event.target.result)
            }
            request.onerror = (event: any) => {
                console.log('查询某一条数据失败')
                console.log(event)
                reject(event)
            }
        })
    }
}
